package at.jku.cp.mir.data;

import java.io.File;
import java.io.IOException;

/**
 * This implementation of a {@link DataFetcher} gets all top tags for an artist
 * and stores them in a file named after the artist.
 * @author clemens
 */
public class ArtistTopTagsFetcher extends DataFetcher
{

	// directory, where the artist top tags shall be placed
	private final File destinationDir;

	public ArtistTopTagsFetcher(File artistFile, File destinationDir)
	{
		super(artistFile);
		this.destinationDir = destinationDir;
	}

	@Override
	protected boolean fetchData(String artist, String lastFmArtist)
	{
		String conform = getFileConformName(artist);
		String fileName = conform + ".xml";
		File artistFile = new File(destinationDir, fileName);
		if (artistFile.exists())
		{
			System.out.println("skipping top tags for artist '" + lastFmArtist + "'");
			return false;
		}

		byte[] data;
		try
		{
			data = communicator.downloadArtistTopTags(lastFmArtist);
		} catch (IOException e)
		{
			throw new IllegalStateException("exception while downloading top tags for artist '" + lastFmArtist + "'", e);
		}

		saveData(data, artistFile);

		System.out.println("fetched top tags for artist '" + lastFmArtist + "'");
		return true;
	}
}
